package com.chat.datea.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.chat.datea.R;
import com.chat.datea.adapter.CollectAdapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.bean.MusicList;
import com.chat.datea.bean.greendao.Collect;
import com.chat.datea.bean.greendao.CollectDao;
import com.chat.datea.ui.MainActivity;
import com.chat.datea.utils.daosingleton.DaoSingleton;

import java.util.ArrayList;

import de.greenrobot.dao.query.QueryBuilder;


/**
 * Created by YanBinXie on 15/11/5.
 */
public class UserCollectFragment extends BaseFragment implements View.OnClickListener {
    private ImageView back;
    private ListView listView;
    private CollectAdapter collectAdapter;
    private ArrayList<Collect> collects;
    private CollectDao collectDao;


    @Override
    protected void initData() {
        collects = new ArrayList<>();
        collectDao = DaoSingleton.getInstance().getCollectDao();
        QueryBuilder qb = collectDao.queryBuilder();
        qb.where(CollectDao.Properties.Want.eq("收藏"));
        collects = (ArrayList<Collect>) qb.list();
        collectAdapter = new CollectAdapter(mActivity, collects);
        listView.setAdapter(collectAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArrayList<MusicList> musicLists = new ArrayList<>();
                for (Collect collect : collects) {
                    MusicList musicList = new MusicList();
                    musicList.setUrl(collect.getUrl());
                    musicList.setAlbumId(collect.getAlbumId());
                    musicList.setFrom(collect.getFrom());
                    musicList.setId(collect.getId());
                    musicList.setName(collect.getName());
                    musicList.setPosition(collect.getPosition());
                    musicList.setSinger(collect.getSinger());
                    musicList.setSongId(collect.getSongId());
                    musicLists.add(musicList);
                }
                MainActivity.play(musicLists, position);
            }
        });
    }

    @Override
    protected void initView() {
        back = (ImageView) view.findViewById(R.id.back);
        back.setOnClickListener(this);
        listView = (ListView) view.findViewById(R.id.list);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_collect, null);

        return view;
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}
